#include "wmatrix.h"
#include "matrix.h"
#include <QVBoxLayout>
#include <QTableWidget>
#include <QMessageBox>
#include <QLabel>


WMatrix::WMatrix(int n)//, QWidget *parent) : QWidget(parent)
{
    error = false;

    //creazione della matrice
    matrix* tmp = new matrix();
    tmp->doMatrix(n); //TODO creazione diretta?

    int** d = tmp->getD();
    int k = tmp->getK();

    if (d==NULL){
        QMessageBox::information(this, tr("Execution Error"),
            tr("One step caused overflow, compromising the result; try a smaller number."));
        error = true;
        return;
    }

    //tabella
    QTableWidget* matrixtab = new QTableWidget(this);

    matrixtab->setColumnCount(k);
    matrixtab->setRowCount(k);

    //popolo la tabella
    QTableWidgetItem* newItem;
    for (int h=0; h<k; h++){
         for (int i=0; i<=h; i++){
             newItem = new QTableWidgetItem(tr("%1").arg(d[h][i]));
             newItem->setTextAlignment(Qt::AlignCenter);
             matrixtab->setItem(h,i,newItem);
         }
    }
    matrixtab->resizeColumnsToContents();
    matrixtab->resizeRowsToContents();
    matrixtab->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //la colonna h parte da 0
    QStringList hcol;
    for (int i=0; i<k; i++) hcol << tr("%1").arg(i);
    matrixtab->setVerticalHeaderLabels(hcol);

    //array M
    QTableWidget* m_array = new QTableWidget(this);
    m_array->setColumnCount(k);
    m_array->setRowCount(1);
    int* m = tmp->getM();

    for (int i=0; i<k; i++){
        newItem = new QTableWidgetItem(tr("%1").arg(m[i]));
        newItem->setTextAlignment(Qt::AlignCenter);
        m_array->setItem(0,i,newItem);
    }
    m_array->resizeColumnsToContents();
    m_array->resizeRowsToContents();
    m_array->setMinimumHeight(3*m_array->rowHeight(0));
    m_array->setMaximumHeight(3*m_array->rowHeight(0));
    m_array->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //tab layout
    QVBoxLayout *layout = new QVBoxLayout();
    layout->addWidget(new QLabel("Matrice D[h][i]"));
    layout->addWidget(matrixtab);
    layout->addWidget(new QLabel("Array delle lunghezze M"));
    layout->addWidget(m_array);
    this->setLayout(layout);

}
